<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
class SessionsController extends Controller
{
   public function __construct()
   {
         $this->middleware('guest',[
           'only'=>['create'],
         ]);

   }
   //登陆操作
   public function create()
   {

      return view('sessions.create');
   }
  //验证表单数据并做登陆处理
  public function store(Request $request)
  {
      $credentials=$this->validate($request,[
       'email'=>'required|email|max:50',
       'password'=>'required',
      ]);
      //验证数据
    if (Auth::attempt($credentials,$request->has("remember"))){
            session()->flash('success', '欢迎回来！');
            return redirect()->intended(route('users.show', [Auth::user()]));
      }else{
            session()->flash('danger', '很抱歉，您的邮箱和密码不匹配');
            return redirect()->back();
     }

  }
  
  //退出登陆
  public function destroy()
  {
     Auth::logout();
     session()->flash('success',"您已成功退出");
     return redirect("login");
  }



}
